package com.purong.purong.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.adapter.HomeArticleAdapter;
import com.purong.purong.adapter.LoanHouseAdapter;
import com.purong.purong.bean.ArticleBean;
import com.purong.purong.bean.ArticleRecommBean;
import com.purong.purong.bean.LoanHouseBean;
import com.purong.purong.bean.LoanHouseListBean;
import com.purong.purong.widget.FullyLinearLayoutManager;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wenjiezeng on 2017/11/17.
 */
public class HouseLoanActivity extends BaseActivity {
    private LoanHouseAdapter mHouseAdapter;
    private HomeArticleAdapter mArticleAdapter;
    private List<LoanHouseBean> houseList=new ArrayList<>();
    private List<ArticleBean> articleList=new ArrayList<>();
    private RecyclerView houseRecycler,articleRecycler;
    @Override
    public int getRootViewId() {
        return R.layout.activity_house_loan;
    }

    @Override
    public void initView() {
        showBack();
        showTitle("房贷");
        houseRecycler=(RecyclerView)findViewById(R.id.house_recycler);

        mHouseAdapter=new LoanHouseAdapter(this);
        mHouseAdapter.setType(LoanHouseAdapter.TYPE_ONE);
        mHouseAdapter.setData(houseList);
        houseRecycler.setLayoutManager(new FullyLinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
        houseRecycler.setAdapter(mHouseAdapter);
        articleRecycler=(RecyclerView)findViewById(R.id.article_recycler);
        mArticleAdapter= new HomeArticleAdapter(this);
        mArticleAdapter.setOnInfoItemClickListener(new HomeArticleAdapter.OnArticleItemClickListener() {
            @Override
            public void onArticleClick(ArticleBean article) {
                Intent intent=new Intent(HouseLoanActivity.this, ArticleDetActivity.class);
                intent.putExtra("ARTICLE_ID",article.getArticle_id());
                startActivity(intent);
            }
        });
        mArticleAdapter.setData(articleList);
        articleRecycler.setLayoutManager(new FullyLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        articleRecycler.setAdapter(mArticleAdapter);
    }
    private void getHouse(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        request(Contant.HOUSE_BANK_LIST, params, true);
    }
    private void getRecommArticle(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("recommend_location","1");
        requestNoToast(Contant.ARTICLE_RECOMMEND, params, true, false);

    }
    @Override
    public void initData() {
        getHouse();
        getRecommArticle();

    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.more:
                Intent intent=new Intent(this,HouseRateActivity.class);
                startActivity(intent);
                break;
        }
    }
    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.HOUSE_BANK_LIST)){
            if (responseInfo.isRequestOk){
                try{
                    LoanHouseListBean bean= JSON.parseObject(responseInfo.content,LoanHouseListBean.class);
                    if (bean!=null&&bean.getBank_list()!=null){
                        houseList.addAll(bean.getBank_list());
                        mHouseAdapter.notifyDataSetChanged();
                    }
                }catch (Exception e){

                }
            }
        }else if (responseInfo.url.equals(Contant.ARTICLE_RECOMMEND)){
            if (responseInfo.isRequestOk){
                try{
                    ArticleRecommBean bean=JSON.parseObject(responseInfo.content,ArticleRecommBean.class);
                    if (bean!=null&&bean.getArticle_recommend_list()!=null){
                        articleList.clear();
                        articleList.addAll(bean.getArticle_recommend_list());
                        mArticleAdapter.notifyDataSetChanged();
                    }

                }catch (Exception e){

                }
            }
        }
    }
}
